package cn.com.ddapp.ddm.inter;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.StringCallback;

import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import okhttp3.Call;

/**
 * Created by caopenghui on 2016-07-30.
 */
public abstract class OkUtilCallback extends StringCallback {

    private EmptyLayout mEmptyLayout;
    private Gson gson = new Gson();

    public OkUtilCallback(){
    }

    public OkUtilCallback(EmptyLayout emptyLayout){
        this.mEmptyLayout = emptyLayout;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        if(mEmptyLayout!=null&&mEmptyLayout.getErrorState()== EmptyLayout.NETWORK_LOADING){
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }
    }

    @Override
    public void onResponse(String response, int id) {

        Entity entity = gson.fromJson(response,Entity.class);
        if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))&&entity.getLogin()&&mEmptyLayout!=null){
            mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
        }

        if(mEmptyLayout!=null){
            mEmptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        }

        onMyResponse(response,id);
    }

    public abstract void  onMyResponse(String response, int id);
}
